package num284;
// Java Iterator interface reference:
// https://docs.oracle.com/javase/8/docs/api/java/util/Iterator.html

import java.util.Iterator;

/**
 * 284. 窥探迭代器
 * 题目：https://leetcode-cn.com/problems/peeking-iterator/
 * 三叶姐题解: https://leetcode-cn.com/problems/peeking-iterator/solution/gong-shui-san-xie-die-dai-qi-ji-ben-ren-b77lz/
 * 进阶
 */
class PeekingIterator2<E> implements Iterator<E> {
	Iterator<E> iter;
	E next;
	public PeekingIterator2(Iterator<E> iterator) {
		iter = iterator;
		if (iter.hasNext()) next = iter.next();
	}

	public E peek() {
		return next;
	}

	@Override
	public E next() {
		E ans = next;
		next = iter.hasNext() ? iter.next() : null;
		return ans;
	}

	@Override
	public boolean hasNext() {
		return next != null;
	}
}
